﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace Common
{
    public class AddNavigation
    {
        public void createNavigation(string linkTitle)
        {

            SPSite site = SPContext.Current.Site;
            {
                using (SPWeb webb = site.OpenWeb(linkTitle))
                {
                    if (!webb.IsRootWeb)
                        webb.Navigation.UseShared = true;

                    if (!webb.ParentWeb.Navigation.UseShared)
                    {
                        SPNavigationNodeCollection topnav = webb.ParentWeb.Navigation.TopNavigationBar;
                        SPNavigationNode node = topnav
                            .Cast<SPNavigationNode>()
                            .FirstOrDefault(n => n.Url.Equals(webb.ServerRelativeUrl));

                        if (node == null)
                        {
                            string LinkTitle = linkTitle;

                            if (LinkTitle.Length > 15)
                            {
                                LinkTitle = LinkTitle.Substring(0, 12) + "...";

                                node = new SPNavigationNode(LinkTitle, webb.ServerRelativeUrl);
                                node = topnav.AddAsLast(node);
                            }
                            else
                            {
                                node = new SPNavigationNode(LinkTitle, webb.ServerRelativeUrl);
                                node = topnav.AddAsLast(node);
                            }
                            node.Update();

                        }
                    }
                }
            }
        }
    }
}
